<?php
/**
 * Created by PhpStorm.
 * User: manarch
 * Date: 2017/11/27
 * Time: 10:01
 */

namespace App\Models;

use Illuminate\Support\Facades\Cache;
use Spatie\Permission\Models\Permission as SpatiePermission;

class Permission extends SpatiePermission
{
    /**
     * @param $name
     * @param $cnName
     * @param int $pid
     */
    public function addPermission($name,$cnName,$pid = 0){
        $this->name = $name;
        $this->cn_name = $cnName;
        $this->pid = $pid;
        $this->save();
    }

    /**
     * @param int $pid
     * @param array $list
     * @return array
     */
    public function getPermissionGroups(){

        $data = $this->getAllByCach();
        $list = [];
        foreach ( $data as $key => $value ){
            if( $value['level'] == 1 ){
                $list['level1'][$value['id']] = $value;
            }
            if( $value['level'] == 2 ){
                $list['level2'][$value['id']] = $value;
            }
            if( $value['level'] == 3 ){
                $list['level3'][$value['id']] = $value;
            }
        }

        foreach ($list['level3'] as $key => $value){
            $list['level2'][$value['pid']]['child'][] = $value;
        }

        foreach ($list['level2'] as $key => $value){
            $list['level1'][$value['pid']]['child'][] = $value;
        }

        return $list['level1'];
    }

    /**
     * @param $id
     * @return mixed
     */
    public function hasChildPermission($id){
        return $this->where('pid',$id)->count();
    }

    /**
     * @return bool
     */
    public function hasParentPermission(){
        return $this->pid != 0;
    }

    /**
     * @param int $pid
     * @param array $list
     * @param int $level
     * @return array
     */
    public function getPermissionList($pid = 0, &$list = [], $level = -1){

        $level++;

        $data = $this->where('pid',$pid)->get();
        foreach ( $data as $value ){
            $value->cn_name = str_repeat('|———', $level).$value->cn_name;
            $list[] = $value;
            $this->getPermissionList($value->id,$list,$level);
        }

        return $list;

    }

    public function getPermissionsGroup(){
        $data = $this->getAllByCach();

        $lists = [];

        foreach ($data as $value){
            $id = $value['id'];
            $lists[$id] = $value;
        }

        foreach ($lists as $list){
            $lists[$list['pid']]['childs'][$list['id']] = &$lists[$list['id']];
        }

        $newlist = isset($lists[0]['childs']) ? $lists[0]['childs'] : [] ;

        return $this->ll($newlist);

    }


    public function ll($data,$pid = 0,&$list = []){

        foreach ($data as $da){
            $da['cn_name'] = str_repeat('|— ', ($da['level']-1)).$da['cn_name'];
            $list[] = $da;
            if(isset($da['childs'])){
                $this->ll($da['childs'],$da['id'],$list);
            }
        }
        return $list;
    }

    /**
     * @param int $pid
     * @return mixed
     */
    public function getChildPermissions($pid = 0){
        return $this->where('pid',$pid)->get();
    }

    private function getAllByCach(){
        if (Cache::has('all-permissions')) {
            return Cache::get('all-permissions');
        }

        $data = $this->all(['id','pid','name','cn_name','level'])->toArray();

        Cache::forever('all-permissions',$data);

        return $data;

    }

}